package leetcodecompetition.D344;

import org.junit.jupiter.api.Test;
import tool.s;

public class T6418 {
    public int[] colorTheArray(int n, int[][] queries) {
        int[] num = new int[n];
        int len = queries.length;
        int[] res = new int[len];
        int sum = 0;
        for (int i = 0; i < len; i++) {
            int index = queries[i][0];
            int color = queries[i][1];
            if (color == num[index]) {
                res[i] = sum;
                continue;
            }
            if (index - 1 >= 0 && num[index - 1] == num[index] && num[index] != 0) {
                sum--;
            }
            if (index + 1 < n && num[index + 1] == num[index] && num[index] != 0) {
                sum--;
            }
            if (index - 1 >= 0 && num[index - 1] == color && color != 0) {
                sum++;
            }
            if (index + 1 < n && num[index + 1] == color && color != 0) {
                sum++;
            }
            num[index]=color;
            res[i] = sum;
        }
        return res;
    }

    @Test
    public void test() {
        int[][] num = {{0, 2}, {1, 2}, {3, 1}, {1, 1}, {2, 1}};
        s.print(colorTheArray(4, num));
    }
}
